package cn.itsource.smartcampus.controller;

import cn.itsource.smartcampus.domain.Teacher;
import cn.itsource.smartcampus.service.TeacherService;
import cn.itsource.smartcampus.util.AjaxResult;
import cn.itsource.smartcampus.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 针对老师的前端控制器
 * 实现crud
 */
@RestController
@RequestMapping("/tea")
public class TeacherController {

    /**
    * 注入的service层 调用业务逻辑的
    * */
    @Autowired
    private TeacherService tservice;

    /**
     * 查询全部的老师信息
     * 请求方式为get
     * @return list的集合
     */
    @GetMapping
    public AjaxResult getAll(){
        List<Teacher> list = tservice.getAll();
        return AjaxResult
                .me()
                .setSuccess(true)
                .setMessage("查询成功")
                .setResultObj(list);
    }

    /**
     * 添加或者修改老师的信息
     * 根据id来判断 有值就是修改
     * 没有就是添加
     * @param tea 页面组装好的一个json格式对象
     * @return 统一的响应结果
     */
    @PutMapping
    public AjaxResult putSaveTeacher(@RequestBody Teacher tea){
        Integer count = null;
        //id不为null 就是修改
        if(tea.getId() != null){
            count = tservice.updateByPrimaryKeySelective(tea);
        }else{
            count = tservice.insert(tea);
        }
        if(count>0){
            return AjaxResult
                    .me()
                    .setSuccess(true)
                    .setMessage("操作成功")
                    .setResultObj(count);
        }
        return null;
    }

    /**
     * 根据主键id删除数据
     * @param id 主键
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult putSaveTeacher(@PathVariable("id") Long id){

        Integer count = tservice.deleteByPrimaryKey(id);
        if(count>0){
            return AjaxResult
                    .me()
                    .setSuccess(true)
                    .setMessage("删除成功")
                    .setResultObj(count);
        }else{
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("删除失败");
        }
    }
    /**
     * @Description: 老师信息导出
     * @Author: Neuronet
     * @Return:
     **/
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Teacher> list = tservice.getAll();
        ExcelUtil.exportExcel(list, "智慧校园管理系统-老师信息", "老师信息表", Teacher.class, "Teachers", response);
    }
    /**
     * @Description: 老师信息导入模板
     * @Author: Neuronet
     * @param response: 响应对象
     * @Return:
     **/
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<Teacher> list = new ArrayList<>();
        ExcelUtil.exportExcel(list, "智慧校园管理系统-老师信息", "老师信息表", Teacher.class, "老师信息导入模板", response);
    }
    /**
     * @Description: 老师信息导入
     * @Author: Neuronet
     * @param file: 要导入的文件
     * @Return:
     **/
    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file) {
        tservice.importExcel(file);
        return AjaxResult.me().setSuccess(true).setMessage("导入成功");
    }
}
